package com.yunkeji.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.dao.domain.UserLoginLog;
import com.yunkeji.dao.mapper.UserLoginLogMapper;
import com.yunkeji.dao.vo.UserLoginLogVo;
import com.yunkeji.system.domain.bo.UserLoginLogBo;
import com.yunkeji.system.service.IUserLoginLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户登录信息Service业务层处理
 *
 * @author zhang
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class UserLoginLogServiceImpl implements IUserLoginLogService {

    private final UserLoginLogMapper baseMapper;

    /** 查询用户登录信息 */
    @Override
    public UserLoginLogVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /** 查询用户登录信息列表 */
    @Override
    public TableDataInfo<UserLoginLogVo> queryPageList(UserLoginLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserLoginLog> lqw = buildQueryWrapper(bo);
        Page<UserLoginLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /** 查询用户登录信息列表 */
    @Override
    public List<UserLoginLogVo> queryList(UserLoginLogBo bo) {
        LambdaQueryWrapper<UserLoginLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserLoginLog> buildQueryWrapper(UserLoginLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserLoginLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserLoginLog::getUserId, bo.getUserId());
        lqw.eq(bo.getLoginTime() != null, UserLoginLog::getLoginTime, bo.getLoginTime());
        lqw.eq(bo.getPlat() != null, UserLoginLog::getPlat, bo.getPlat());
        lqw.eq(bo.getAgent() != null, UserLoginLog::getAgent, bo.getAgent());
        lqw.eq(bo.getVersionIndex() != null, UserLoginLog::getVersionIndex, bo.getVersionIndex());
        lqw.eq(bo.getMerchants() != null, UserLoginLog::getMerchants, bo.getMerchants());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), UserLoginLog::getModel, bo.getModel());
        lqw.eq(
                StringUtils.isNotBlank(bo.getDeviceToken()),
                UserLoginLog::getDeviceToken,
                bo.getDeviceToken());
        lqw.eq(StringUtils.isNotBlank(bo.getAddr()), UserLoginLog::getAddr, bo.getAddr());
        lqw.eq(
                StringUtils.isNotBlank(bo.getCityCode()),
                UserLoginLog::getCityCode,
                bo.getCityCode());
        lqw.eq(
                StringUtils.isNotBlank(bo.getMoProvince()),
                UserLoginLog::getMoProvince,
                bo.getMoProvince());
        return lqw;
    }

    /** 新增用户登录信息 */
    @Override
    public Boolean insertByBo(UserLoginLogBo bo) {
        UserLoginLog add = BeanUtil.toBean(bo, UserLoginLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /** 修改用户登录信息 */
    @Override
    public Boolean updateByBo(UserLoginLogBo bo) {
        UserLoginLog update = BeanUtil.toBean(bo, UserLoginLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /** 保存前的数据校验 */
    private void validEntityBeforeSave(UserLoginLog entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /** 批量删除用户登录信息 */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
